#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <string>

int main(int argc, char *argv[])
{
    if (argc != 2) {
        fprintf(stderr, "+ fn\n");
        return 1;
    }

        /* handle the line  */
    int     line_size;
    char   *lineptr          = NULL;
    size_t  line_buffer_size = 0;

    FILE *fp = fopen(argv[1], "r");
    if (fp == NULL) {
        fprintf(stderr, "open `%s' failed: %s\n", argv[1], strerror(errno));
        return 1;
    }

    bool after_head_word_p = false;
    while ((line_size = getline(&lineptr, &line_buffer_size, fp)) > 0) {
        if (after_head_word_p) {
            after_head_word_p = false;

            char *p = strchr(lineptr, ' ');
            if (p) {
                std::string key(lineptr, p - lineptr);
                std::string oth(p + 1);
                printf("%s\n", key.c_str());
                fputs(oth.c_str(), stdout);
            }
            else
                fputs(lineptr, stdout);
        }
        else {
            if (strstr(lineptr, "---headword---"))
                after_head_word_p = true;
            fputs(lineptr, stdout);
        }
    }

    return 0;
}
